package engine.core
{
	import flash.utils.*;
	
	public class RxTick
	{
		private static const FRAMES_TO_AVERAGE:int = 15;
		private static var s_currentTime:int;
		private static var s_deltaTime:int;
		private static var s_totalTime:int;
		private static var s_frame:int;
		private static var s_frameTime:Number;
		private static var s_frameTimesArray:Vector.<Number>;
		private static var s_frameTimeArrayIndex:int;
		private static var s_avgRunningTotal:Number;
		private static var s_avgFrameTime:Number;
		
		public function RxTick()
		{
		}
		
		public static function Initialize() : void
		{
			s_currentTime = getTimer();
			s_deltaTime = 0;
			s_totalTime = 0;
			s_frame = -1;
			s_frameTime = 0;
			s_frameTimesArray = new Vector.<Number>(FRAMES_TO_AVERAGE, true);
			var i:int = 0;
			while (i < FRAMES_TO_AVERAGE)
			{
				
				s_frameTimesArray[i] = 0;
				i++;
			}
			s_frameTimeArrayIndex = 0;
			s_avgRunningTotal = 0;
			s_avgFrameTime = 0;
		}
		
		public static function Update() : void
		{
			var timer:int = getTimer();
			s_deltaTime = timer - s_currentTime;
			s_totalTime = s_totalTime + s_deltaTime;
			s_currentTime = timer;
			if (s_deltaTime > 2000)
			{
				s_deltaTime = 2000;
			}
			s_frameTime = Number(s_deltaTime) * 0.001;
			s_frame = s_frame + 1;
			s_avgRunningTotal = s_avgRunningTotal + (s_frameTime - s_frameTimesArray[s_frameTimeArrayIndex]);
			s_frameTimesArray[s_frameTimeArrayIndex] = s_frameTime;
			s_frameTimeArrayIndex = (s_frameTimeArrayIndex + 1) % FRAMES_TO_AVERAGE;
			s_avgFrameTime = s_avgRunningTotal / Number(FRAMES_TO_AVERAGE);
		}
		
		public static function get totalTime() : int
		{
			return s_totalTime;
		}
		
		public static function get frame() : int
		{
			return s_frame;
		}
		
		public static function get frameTime() : Number
		{
			return s_frameTime;
		}
		
		public static function set frameTime(time:Number) : void
		{
			s_frameTime = time;
		}
		
		public static function get avgFrameTime() : Number
		{
			return s_avgFrameTime;
		}
	}
}